<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> 
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>

<script runat="server">
    
    private const string webAppPropertyKey = "Custom404Path";
    
    protected override void OnLoad(EventArgs e) {

      if (Page.IsPostBack != true)
      {
          //***************************************************
          //get a reference to the farm and web service(WFE)
          //we will use these to iterate through the web applications
          //and build the web application list
          SPFarm farm = SPFarm.Local;
          SPWebService service = farm.Services.GetValue<SPWebService>("");

          //Add a default Item to the list
          ListItem defaultItem = new ListItem("Select a web application from the list", "0");
          lstWebApplications.Items.Add(defaultItem);

          foreach (SPWebApplication webApp in service.WebApplications)
          {   
            ListItem webAppItem = new ListItem(webApp.DisplayName, webApp.Id.ToString(), true);
            lstWebApplications.Items.Add(webAppItem);
          }          
      }
          
  }
  public void getCustom404Path(object sender, System.EventArgs e)
    {
        lblMessage.Text = "";
        txtCustom404Path.Text = "";
      
        if (lstWebApplications.SelectedIndex != 0)
        {
            SPFarm farm = SPFarm.Local;
            SPWebService service = farm.Services.GetValue<SPWebService>("");

            Guid webAppGuid = new Guid(lstWebApplications.SelectedValue);
            SPWebApplication webApp = service.WebApplications[webAppGuid];

            if (webApp.Properties.ContainsKey(webAppPropertyKey))
            {
                txtCustom404Path.Text = (string)webApp.Properties[webAppPropertyKey];
            }
            btnUpdate404Path.Enabled = true;
        }
        else
        {
            txtCustom404Path.Text = "";
            btnUpdate404Path.Enabled = false;
        }
        
    }

    public void updateCustom404Path(object sender, System.EventArgs e)
    {
        SPFarm farm = SPFarm.Local;
        SPWebService service = farm.Services.GetValue<SPWebService>("");

        Guid webAppGuid = new Guid(lstWebApplications.SelectedValue);
        SPWebApplication webApp = service.WebApplications[webAppGuid];

        //create a web application property to store the 
        //path to the custom 404 page
        if (webApp.Properties.ContainsKey(webAppPropertyKey))
        {
            webApp.Properties[webAppPropertyKey] = txtCustom404Path.Text;
            lblMessage.Text = "Changes saved";
        }
        else
        {
            webApp.Properties.Add(webAppPropertyKey, txtCustom404Path.Text);
            lblMessage.Text = "Changes saved";
        }
        //update the FileNotFound property of the web application
        //this needs to be an html page in the LAYOUTS/1033
        webApp.FileNotFoundPage = "Custom404Page.htm";

        //update the web application
        webApp.Update();
    }
</script>

<asp:Content ID="Main" runat="server" contentplaceholderid="PlaceHolderMain" >
    <table>
    <tr>
        <td class="ms-descriptionText">
            Web Application:
        </td>
        <td>
            <asp:DropDownList ID="lstWebApplications" runat="server" AutoPostBack="true" OnSelectedIndexChanged="getCustom404Path" width="300px"></asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td class="ms-descriptionText">
            Custom 404 Path:
        </td>
        <td>
            <asp:TextBox ID="txtCustom404Path" runat="server" width="300px" />
        </td>
    </tr>
    <tr>
        <td colspan="2" class="ms-descriptionText">
           <asp:Button ID="btnUpdate404Path" runat="server" OnClick="updateCustom404Path" Text="OK" Enabled="false" />
           &nbsp;&nbsp;
           <b><asp:Label ID="lblMessage" runat="server" /></b>
        </td>
    </tr>
    </table>
    
</asp:Content>

<asp:Content ID="PageTitle" runat="server" contentplaceholderid="PlaceHolderPageTitle" >
  Custom 404 Settings
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" runat="server" contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
  Custom 404 Settings
</asp:Content>


